<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Manage_campaign extends CI_Controller{
		
		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_campaign");
				$this->load->model("apanel/db_news");
			$this->load->library('pagination');
		}
		
		public function index(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			redirect('manage_campaign/campaign_list');
		}
		
		public function campaign_list($st = ""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			$scripts = '
			<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
			$(\'#table1\').dataTable( {
			"bJQueryUI": true,
			"bProcessing": true,
			"bServerSide": true,
			"sAjaxSource": "'.base_url().'manage_campaign/datalist",
			"iDisplayStart"  : 0,
			"sPaginationType": "full_numbers",
			"aaSorting": [[ 0, "desc" ]],
			"aoColumnDefs": [
			{ "bSortable": false, "aTargets": [ 0 ] },
			{ "bSortable": false, "aTargets": [ 3 ] },
			{ "bSortable": false, "aTargets": [ 4 ] },
			],
				"fnServerData": function(sSource, aoData, fnCallback)
			{
			$.ajax(
			{
			\'dataType\': \'json\',
			\'type\'  : \'POST\',
			\'url\'    : sSource,
			\'data\'  : aoData,
			\'success\' : fnCallback
			}
			);
			}
			} );
			} );
			</script>';
				
			//== template
			$this->cismarty->assign("scripts",$scripts);
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,8));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_campaign/campaign_delAll'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/campaign/campaign_list.html');
		}
		
		public function datalist(){
			$output = $this->db_campaign->datalist();
			echo json_encode($output);
		}
		
		public function campaign_add(){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post("bntSubmit") != null){
				$this->form_validation->set_rules("name","Name","required");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("detail","Detail",'trim');
			

				if($this->form_validation->run() == FALSE){

	
						
					//set error
					$this->cismarty->assign("errorcustomer",form_error('customer'));
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("errordetail",form_error('detail'));
				
						if($_FILES['pic']['tmp_name'] == ""){
							$er="The Banner field is required.";
							$this->cismarty->assign("errornopic",$er);
						}

					
	
				}else{
					$error = false;
					$picname="";
			
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/campaign/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 500;
							$config['height'] = 320;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					
			
					if($_FILES['pic']['tmp_name'] == ""){
							$er="The Banner field is required.";
							$this->cismarty->assign("errornopic",$er);
							$error = true;
						}
					
					if(!$error){
						$data = array(
								"customer_pkid"=>$this->input->post('customer'),
								"name"=>$this->input->post('name'),
								"picture"=>$picname,
								"detail"=>$this->input->post('detail')
						);
						$res =$this->db->insert('mobile_campaign',$data);
						if($res) redirect('manage_campaign/campaign_list/success');
						else  redirect('manage_campaign/campaign_list/fail');
					}
				}
			
			}
			//== template
			$this->cismarty->assign("customer",$this->db_news->customer_list());
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,8));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart('manage_campaign/campaign_add'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/campaign/campaign_add.html');
		}
		
		public function campaign_edit($id,$cid=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post("bntSubmit") != null){
				$this->form_validation->set_rules("name","Name","required");
				$this->form_validation->set_rules("customer","Customer","required");
				$this->form_validation->set_rules("detail","Detail",'trim');
			

				if($this->form_validation->run() == FALSE){

	
						
					//set error
					$this->cismarty->assign("errorcustomer",form_error('customer'));
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("errordetail",form_error('detail'));
				}else {
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/campaign/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 500;
							$config['height'] = 320;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							//update picture
							$uppic = array('picture'=>$picname);
							$this->db->where('pkid',$id);
							$this->db->update('mobile_campaign',$uppic);
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"customer_pkid"=>$this->input->post('customer'),
								"name"=>$this->input->post('name'),
								"detail"=>$this->input->post('detail')
						);
						$this->db->where('pkid',$id);
						$res =$this->db->update('mobile_campaign',$data);
						if($res) redirect('manage_campaign/campaign_list/success');
						else  redirect('manage_campaign/campaign_list/fail');
					}
				}
					
			}else {
				$this->db->where("pkid",$id);
				$res = $this->db->get('mobile_campaign');
				$row = $res->row();
				$this->cismarty->assign("name",$row->name);
				$this->cismarty->assign("detail",$row->detail);
				$group 			= $row->customer_pkid;
				if(trim($row->picture) != "")
					$this->cismarty->assign("img",'<img src="'.base_url().'media/campaign/'.$row->picture.'"><br>');
			}
			//== template
			$this->cismarty->assign("customer",$this->db_news->customer_list($group));
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(6,8));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_campaign/campaign_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/campaign/campaign_edit.html');
		}
		
		public function campaign_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_campaign");
				if($res) redirect('manage_campaign/campaign_list/success');
			}
		}
		
		public function campaign_delAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_campaign");
				}
			}
			redirect('manage_campaign/campaign_list/success');
		}
		
		
		
	}
?>